package com.epfresh.supplier.adapter;

import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;

import com.epfresh.api.universalimageloader.core.ImageLoader;
import com.epfresh.api.universalimageloader.core.assist.ImageSize;
import com.epfresh.api.universalimageloader.core.imageaware.ImageViewAware;
import com.epfresh.api.widget.PhotoUploadView;
import com.epfresh.supplier.R;
import com.epfresh.supplier.entity.Images;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class GridViewAdapter extends BaseAdapter {
    private Context context;
    private List<Images> strList;
    private int hidePosition = AdapterView.INVALID_POSITION;
    private int imgMaxSize = 6;

    public GridViewAdapter(Context context, List<Images> strList) {
        this.context = context;
        this.strList = strList;
    }

    public int getImgMaxSize() {
        return imgMaxSize;
    }

    public void setImgMaxSize(int imgMaxSize) {
        this.imgMaxSize = imgMaxSize;
    }

    @Override
    public int getCount() {
        return strList.size();
    }

    @Override
    public Images getItem(int position) {
        return strList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        PhotoUploadView view;
        if (convertView == null) {
            view = (PhotoUploadView) LayoutInflater.from(context).inflate(R.layout.item_photo_view, null, false);

        } else {
            view = (PhotoUploadView) convertView;
        }

        Images images = strList.get(position);
        int type = images.getType();


//        view.setUrl(images.getImageUrl());
//        view.setFilePath(images.getFilePath());
        ImageSize imageSize = new ImageSize(200, 200);
        String path = "";
        view.setPhotoId(images.getAttachmentId());
        if (images.getFilePath() != null && !"".equals(images.getFilePath())) {
            path = images.getFilePath();
            ImageLoader.getInstance().displayImage(Uri.fromFile(new File(path)).toString(), new ImageViewAware(view.getImagView()), view.getImageOption(), imageSize, null, null);
            view.showImage();
        } else if (images.getPath() != null) {
            path = images.getPath();

//            view.setUrl(images.getImageUrl());
            ImageLoader.getInstance().displayImage(path, new ImageViewAware(view.getImagView()), view.getImageOption(), imageSize, null, null);
            view.showImage();
        }else if (!TextUtils.isEmpty(images.getUrl())){
            path = images.getUrl();
            ImageLoader.getInstance().displayImage(path, new ImageViewAware(view.getImagView()), view.getImageOption(), imageSize, null, null);
            view.showImage();
        }
        else {
            ImageLoader.getInstance().displayImage(path, new ImageViewAware(view.getImagView()), view.getImageOption(), imageSize, null, null);
            view.showAddView();
        }

        if (type == Images.LOAD_FAIL) {
            view.showFail();
        } else if (type == Images.SUCCESS) {
            view.showView();
        } else if (type == Images.LOADING) {
            view.showProgress();
        } else {
            view.hideProgress();
        }

        //hide时隐藏Text
        if (position != hidePosition) {
            view.setVisibility(View.VISIBLE);
        } else {
            view.hideView();
        }
        if (images.isAdd()) {
            view.hideTop();
//            view.setBackgroundResource(R.mipmap.upload_bg);
        } else {
//            view.setBackgroundResource(R.drawable.def_img);
            view.showTop();
        }
        view.setId(position);
//        view.getvTop().setTag(R.id.item_key_position,position);
        view.getvTop().setTag(R.id.item_key_position, images.getUuid());

        view.getvTop().setOnClickListener(deleteClick);
        Log.e("image", "i=" + position + "-----image=" + images);
        return view;
    }

    View.OnClickListener deleteClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Object tag = v.getTag(R.id.item_key_position);
            if (tag instanceof String) {
                String uuid = (String) tag;
                int index = findImage(uuid);
                boolean isAdd = false;
                if (strList.size() == imgMaxSize) {
                    Images images = strList.get(imgMaxSize - 1);
                    if (!images.isAdd())
                        isAdd = true;
                }
                if (index > -1) {
                    strList.remove(index);
                    int i = strList.size() - 1;

                    if (isAdd) {
                        Images images = new Images();
                        strList.add(images);
                    }
                    notifyDataSetChanged();
                    if (onDeleteListener != null){
                        onDeleteListener.onClick(v);
                    }
                }

            }
        }
    };


    private View.OnClickListener onDeleteListener;

    public View.OnClickListener getOnDeleteListener() {
        return onDeleteListener;
    }

    public void setOnDeleteListener(View.OnClickListener onDeleteListener) {
        this.onDeleteListener = onDeleteListener;
    }

    public void hideView(int pos) {
        hidePosition = pos;
        notifyDataSetChanged();
    }

    public void showHideView() {
        hidePosition = AdapterView.INVALID_POSITION;
        notifyDataSetChanged();
    }

    public void removeView(int pos) {
        strList.remove(pos);
        notifyDataSetChanged();
    }

    //更新拖动时的gridView
    public void swapView(int draggedPos, int destPos) {
        Images images = strList.get(destPos);
        if (images.isAdd()) {
            return;
        }
        //从前向后拖动，其他item依次前移
        if (draggedPos < destPos) {
            strList.add(destPos + 1, getItem(draggedPos));
            strList.remove(draggedPos);
        }
        //从后向前拖动，其他item依次后移
        else if (draggedPos > destPos) {
            strList.add(destPos, getItem(draggedPos));
            strList.remove(draggedPos + 1);
        }
        hidePosition = destPos;
        notifyDataSetChanged();
    }

    public void addedView() {
        int size = strList.size();
        if (size < imgMaxSize) {

            if (size == 0) {
                Images images = new Images();
                strList.add(images);
            } else {
                Images images1 = strList.get(size - 1);
                if (!images1.isAdd()) {
                    Images images = new Images();
                    strList.add(images);
                }
            }
        }
        notifyDataSetChanged();
    }

    public void clearAllStatus() {
        for (int i = 0; i < strList.size(); i++) {
            Images images1 = strList.get(i);
            images1.setAdd(false);
        }
    }


    public void updateStatus(String uuid, String id, int type) {
        if (uuid != null && !"".equals(uuid)) {
            for (int i = 0; i < strList.size(); i++) {
                Images images1 = strList.get(i);
                String uuid1 = images1.getUuid();
                if (uuid.equals(uuid1)) {
                    images1.setType(type);
                    Log.e("image", "uuid:" + uuid);
                    Log.e("image", "type:" + type);
                    if (type != Images.DEFAULT) {
                        images1.setId(Integer.parseInt(id));
                        Log.e("image", "id:" + id);
                        images1.setImageId(id);
                        images1.setAttachmentId(id);
                    }
                }
            }
            notifyDataSetChanged();
        }

    }

    public int findImage(String uuid) {
        if (uuid != null && !"".equals(uuid)) {
            for (int i = 0; i < strList.size(); i++) {
                Images images1 = strList.get(i);
                String uuid1 = images1.getUuid();
                if (uuid.equals(uuid1)) {
                    return i;
                }
            }
        }

        return -1;

    }

    public List<Images> getImages() {
        List<Images> imgs = new ArrayList<>();
        for (int i = 0; i < strList.size(); i++) {
            Images images1 = strList.get(i);
            if (!images1.isAdd() && images1.isUploadSuccess()) {
                imgs.add(images1);
            }
        }

        return imgs;
    }

    public boolean isUploadNotSuccess() {

        for (int i = 0; i < strList.size(); i++) {
            Images images1 = strList.get(i);
            if (!images1.isAdd() && !images1.isUploadSuccess()) {
                return true;
            }
        }

        return false;
    }
}
